﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using Microsoft.Phone.UserData;
using Contact_Sync.ServiceReference1;


namespace Contact_Sync
{
    public partial class MainPage : PhoneApplicationPage
    {

        // Delclaring Project Objects
        Service1Client client = new Service1Client("BasicHttpBinding_IService1");
        Contacts c = new Contacts();
        sContact ci = new sContact();
        PhoneNumberChooserTask phoneNumberChooserTask;
        
       
        
        public MainPage()
        {
        
            InitializeComponent();

           String.Format("{0:(###)#######}",textBox1);
            if (slider1.IsEnabled == true)
            {
                if (checkBox1.IsChecked != true)
                {
                    // Searching Contacts From Phone
                    c.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(c_SearchCompleted);
                    c.SearchAsync(String.Empty, FilterKind.None, null);
                    
                }
            }
            
        }



        
        void c_SearchCompleted(object sender, ContactsSearchEventArgs e)
        {

            foreach (var result in e.Results)

          
            {
                 try
                    {
                        //Send Data form phone to WCF WebService
                    ci.Name = result.DisplayName;
                    ci.Phno = result.PhoneNumbers.FirstOrDefault().PhoneNumber.ToString();
                    client.SetdataAsync(ci);
                    
                    
                    }
                    catch { NullReferenceException ex; }
         
                
            }
            
        }




        private void textBox1_GotFocus(object sender, RoutedEventArgs e)
        {
            textBox1.Foreground = new SolidColorBrush(Colors.Black);
            textBox1.Text = "";
                       
        }
        // Search Box 
        private void textBox1_LostFocus(object sender, RoutedEventArgs e)
        {
            textBox1.Foreground = new SolidColorBrush(Colors.Gray);
            this.textBox1.Text = "Search";
        }




        //Get Data From WCF webservice 
        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                client.GetDataAsync();
                client.GetDataCompleted += new EventHandler<GetDataCompletedEventArgs>(client_getdata);
            }

        }
        public void client_getdata(object Sender, GetDataCompletedEventArgs e)
        {
            foreach (var result in e.Result)
            {
                try
                {

                    var con = from m in e.Result
                              where m.Phno == textBox1.Text
                              select new sContact
                              {
                                  Name = m.Name,
                                  Phno = m.Phno
                              };


                    SecondListBox.ItemsSource = con;

                }
                catch { WebException ex; }
            }

        }




        // Choose Contacts From Phone 
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            phoneNumberChooserTask = new PhoneNumberChooserTask();
            phoneNumberChooserTask.Completed += new EventHandler<PhoneNumberResult>(phoneNumberChooserTask_Completed);

            try
            {
                phoneNumberChooserTask.Show();
            }
            catch (System.InvalidOperationException ex)
            {
                MessageBox.Show("An error occurred.");
            }
        }
        void phoneNumberChooserTask_Completed(object sender, PhoneNumberResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                MessageBox.Show("The phone number for " + e.DisplayName + " is " + e.PhoneNumber);

                //Code to start a new call using the retrieved phone number.
                PhoneCallTask phoneCallTask = new PhoneCallTask();
                phoneCallTask.DisplayName = e.DisplayName;
                phoneCallTask.PhoneNumber = e.PhoneNumber;


                phoneCallTask.Show();

            }

        }




        // Tap on displayed number  
        private void TextBlock_Tap(object sender, GestureEventArgs e)
        {
            
            PhoneCallTask call = new PhoneCallTask();
            call.PhoneNumber = textBox1.Text;
            call.Show();
        }

      
        

        


        
    }      
       
}
        
